//
//  WZGAfterAplyStatusVC.m
//  ZCJZ
//
//  Created by 潘洪波 on 2020/2/27.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "WZGAfterAplyStatusVC.h"
#import "WZGAplyFillInformationVC.h"
#import "WZGAplyDetailVC.h"
#import "WZGAplyToShopkeeperVC.h"
#import "SKBaseWebService.h"
#import <MJExtension.h>
@implementation WZGAfterAplyStatusModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{@"id":@"idstr"};
}

@end

@interface WZGAfterAplyStatusVC ()

@property (nonatomic, strong) UIImageView *statImgView ;
@property (nonatomic, strong) UILabel *btnTitleLabel ;
@property (nonatomic, strong) UILabel *statReasonLabel ;
@property (nonatomic, strong) UILabel *statTitleLabel ;
@property (nonatomic, strong) WZGAfterAplyStatusModel *statusModel ;

@end


@implementation WZGAfterAplyStatusVC

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navTitle = @"提交认证";
    [self getAplyDetail];
}

// 注销账号（退出登录）
- (void)dismissVC{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
    manager.userModel = nil;
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    [ud setValue:nil forKey:@"userinfo"];
    [ud setValue:nil forKey:@"upassTF"];
    [ud setValue:nil forKey:@"accessToken"];
    
    [ud removeObjectForKey:@"userinfo"];
    [ud removeObjectForKey:@"upassTF"];
    [ud removeObjectForKey:@"accessToken"];
    
    [ud synchronize];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"homereload" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"catereload" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"mainshoppingnum" object:@(0)];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:^{
        [self.tabBarController setSelectedIndex:0];
    }];
}

- (void)setStatus:(kAfterAplyStatus)status{
    _status = status;
    switch (status) {
        case kAfterAplyStatusFailure:
            self.statImgView.image = [UIImage imageNamed:@"aply_failure"];
            self.btnTitleLabel.text = @"重新提交";
            [self setRightItemWithTitle:@"取消" action:@selector(dismissVC)];
            break;
        case kAfterAplyStatusSuccess:
            self.statImgView.image = [UIImage imageNamed:@"aply_success"];
            self.btnTitleLabel.text = @"我知道了";
            break;
        case kAfterAplyStatusWaiting:
            self.statImgView.image = [UIImage imageNamed:@"aply_wating"];
            self.btnTitleLabel.text = @"查看认证资料";
            [self setRightItemWithTitle:@"取消" action:@selector(dismissVC)];
            break;
        default:
            self.statImgView.image = nil;
            
            break;
    }
}

- (void)getAplyDetail{
    WeakSelf(self);
    [SKBaseWebService getRequest:@"/v1/member-upgrade-apply/detail" parameters:[NSMutableDictionary new] progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        if (status == kHXSNoError) {
            weakself.statusModel = [WZGAfterAplyStatusModel mj_objectWithKeyValues:data];
        }
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        
    }];
}

- (void)setStatusModel:(WZGAfterAplyStatusModel *)statusModel{
    _statusModel = statusModel;
    if (statusModel) {
        self.statReasonLabel.text = statusModel.reason;
        self.statTitleLabel.text = statusModel.status_name;
        self.status = statusModel.status;
    }
}

- (void)bottomBtnAction{
    switch (self.status) {
        case kAfterAplyStatusFailure://重新申请
        {
//            WZGAplyFillInformationVC *vc = [WZGAplyFillInformationVC new];
//            vc.type = _statusModel.channel + 1;
//            vc.navTitle = @"提交认证";
//            [self.navigationController pushViewController:vc animated:YES];
            [self dismissViewControllerAnimated:YES completion:^{
                
                WZGAplyToShopkeeperVC *vc = [WZGAplyToShopkeeperVC new];
                vc.navTitle = @"提交认证";
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                
                SYBaseViewController * curVC = (SYBaseViewController*)[generalToolBox getcurrentVC];
                [curVC presentViewController:nav animated:YES completion:nil];
            }];
            
            
        }
            break;
        case kAfterAplyStatusSuccess:
        {
            WeakSelf(self);
            [self netFetchMemberInfo:^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"loginSuccess" object:nil];
                [weakself dismissViewControllerAnimated:YES completion:nil];
            }];
        }
            break;
        case kAfterAplyStatusWaiting:
        {
            WZGAplyDetailVC *vc = [WZGAplyDetailVC new];
            [vc setModel:self.statusModel];
            vc.navTitle = @"认证详情";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)netFetchMemberInfo:(void(^)(void))complete {
    __weak typeof(self) weakSelf = self;
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [SKBaseWebService getRequest:@"/v1/member/member-info" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        NSString *token = weakSelf.userModel.member.accessToken;
        
        weakSelf.userModel.member = [ZCMember mj_objectWithKeyValues:[data valueForKey:@"member"]];
        weakSelf.userModel.memberProfile = [ZCMemberProfile mj_objectWithKeyValues:[data valueForKey:@"memberProfile"]];
        weakSelf.userModel.member.accessToken = token;
        ZCBaseUserManager *manager = [ZCBaseUserManager shareInstance];
        manager.userModel = weakSelf.userModel;
        
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud setValue:weakSelf.userModel.mj_keyValues forKey:@"userinfo"];
        [ud setValue:weakSelf.userModel.member.accessToken forKey:@"accessToken"];
        [ud synchronize];
        if (complete) {
            complete();
        }
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    }];
}

@end
